package com.uday.frmwrk.service;

import java.util.List;
import java.util.Map;

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;

import com.uday.frmwrk.data.BaseRequestItem;
import com.uday.frmwrk.data.BaseResponseItem;
import com.uday.frmwrk.dataaccess.BaseTrasactionAwareService;

public class BaseService extends BaseTrasactionAwareService {
	private Map<String, List<BaseActivity>> mapActivities;

	public BaseResponseItem execute(final BaseRequestItem objBaseRequestItem) {
		final BaseResponseItem objBaseResponseItem = new BaseResponseItem();
		transactionTemplate.execute(new TransactionCallback() {
			public Object doInTransaction(TransactionStatus status) {
				try {
					if (mapActivities.keySet().contains(
							objBaseRequestItem.getStrActivityMap())) {
						List<BaseActivity> lstCurrActivities = mapActivities
								.get(objBaseRequestItem.getStrActivityMap());
						
						for(int indexActivity = 0;indexActivity < lstCurrActivities.size();indexActivity++){
							lstCurrActivities.get(indexActivity).process(objBaseRequestItem, objBaseResponseItem);
						}
					} else {
						throw new Exception("No Activity map corresponding to "
								+ objBaseRequestItem.getStrActivityMap()
								+ " found.");
					}
				} catch (Exception e) {
					objBaseResponseItem.getLstErrorList().add(e.getMessage());
					e.printStackTrace();
					status.setRollbackOnly();
				}
				return objBaseResponseItem;
			}
		});

		return objBaseResponseItem;
	}

	/**
	 * @return the mapActivities
	 */
	public Map<String, List<BaseActivity>> getMapActivities() {
		return mapActivities;
	}

	/**
	 * @param mapActivities the mapActivities to set
	 */
	public void setMapActivities(Map<String, List<BaseActivity>> mapActivities) {
		this.mapActivities = mapActivities;
	}
}
